#include<bits/stdc++.h>
using namespace std;
#define int long long

vector<int> days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int year, month, day, hour, minute, second, x;

int computeDeltaMinute() {
    int ans = 0;

    for(int i = 1970; i < year; i++) {
        if(i % 4 == 0 && i % 100 || i % 400 == 0)
            ans += 366;
        else ans += 365;
    }

    if(year % 4 == 0 && year % 100 || year % 400 == 0)
        days[2] = 29;
    else days[2] = 28;

    for(int i = 1; i < month; i++)
        for(int j = 1; j <= days[i]; j++)
            ans++;

    ans = (ans + day - 1) * 24 * 60 + hour * 60 + minute;

    return ans;
}


void computeAns(int deltaMin) {
    second = 0;

    if(year % 4 == 0 && year % 100 || year % 400 == 0)
        days[2] = 29;
    else days[2] = 28;


    int deltaHour = deltaMin / 60;
    deltaMin %= 60;

    if(minute >= deltaMin) {
        minute -= deltaMin;
    } else {
        minute = 60 - deltaMin + minute;
        hour--;
    }

    if(hour >= deltaHour) {
        hour -= deltaHour;
    } else {
        hour = 24 - deltaHour + hour;
        day--;
    }

    if(day < 1) {
        month--;
        day = days[month];
    }
}


signed main() {
//	system("start https://www.lanqiao.cn/problems/19730/learning/");

    int t;
    scanf("%lld", &t);

    while(t--) {

        scanf("%lld-%lld-%lld %lld:%lld:%lld %lld", &year, &month, &day, &hour, &minute, &second, &x);

        int deltaMinute = computeDeltaMinute();

        int subtractMinute = deltaMinute % x;

        computeAns(subtractMinute);
        printf("%04lld-%02lld-%02lld %02lld:%02lld:%02lld\n", year, month, day, hour, minute, second);
    }

    return 0;
}

